<?php

class UserController extends Controller
{
	public function show()
	{
		echo "login page...";
		//$this->display();
	}

	/**
	 * listbook
	 */
	public function listbook()
	{
		$page	=	Lib::get('page','int');
		$list	=	Book::getList($page);
		
		$this->assign('list', $list);
		$this->display();
	}
	/**
	 * 查看指定book信息
	 */
	public function book()
	{
		$bookid	=	Lib::get('id','int');
		if(!$bookid)
		{
			Show::notice("无此书籍信息");
		}
		$book	=	Book::getBook($bookid);
		if(!$book)
		{
			Show::go400("无此书籍信息");
		}
		
		$this->assign('book', $book);
		$this->display();
	}
	
	public function addbook()
	{
		if(!empty($_POST))
		{
			//todo: 可支持上传zip或tar格式的文本
			if(empty($_FILES['book']) || $_FILES['book']['error'] || ('text/plain'!=$_FILES['book']['type']) )
			{
				Show::notice("请上传正确的文本书籍", Lib::url('./?a=user.addbook'));
			}
			
			$cont	=	file_get_contents($_FILES['book']['tmp_name']);
			$cont	=	StringHelper::encode($cont);
			$path	=	Book::storeBook($cont);
			
			$bookid	=	Book::addBook(array(
				'title'		=>	Lib::post('title'),
				'desc'		=>	Lib::post('desc'),
				'path'		=>	$path,
				'addtime'	=>	time(),
				'length'	=>	strlen($cont),
			));
			
			$this->redirect("./?a=user.book&id={$bookid}");
		}
		
		$this->display();
	}
	
	/**
	 * 编辑书籍信息
	 */
	public function editbook()
	{
		$bookid	=	Lib::get('id','int');
		if(!$bookid)
		{
			Show::notice("无此书籍信息");
		}
		$book	=	Book::getBook($bookid);
		if(!$book)
		{
			Show::go400("无此书籍信息");
		}
		
		if(!empty($_POST))
		{
			Book::editBook($bookid, array(
				'title'		=>	Lib::post('title'),
				'desc'		=>	Lib::post('desc'),
			));
			
			$this->redirect("./?a=user.book&id={$bookid}");
		}
		
		$this->assign('book', $book);
		$this->display();
	}
	
	
}

?>